package org.blame.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.blame.constants.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
//        1.建立连接
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost(Constants.HOST);
        connectionFactory.setPort(Constants.PORT);
        connectionFactory.setUsername(Constants.USER_NAME);
        connectionFactory.setPassword(Constants.PASSWORD);
        connectionFactory.setVirtualHost(Constants.VIRTUAL);

        Connection connection= connectionFactory.newConnection();

        //        2.开启信道
        Channel channel=connection.createChannel();

//        申明交换机
        channel.exchangeDeclare(Constants.DIRECT_EXCHANGE_NAME,BuiltinExchangeType.DIRECT,true);

//        申明队列
        channel.queueDeclare(Constants.DIRECT_QUEUE_NAME1,true,false,false,null);
        channel.queueDeclare(Constants.DIRECT_QUEUE_NAME2,true,false,false,null);

//        队列的绑定
        channel.queueBind(Constants.DIRECT_QUEUE_NAME1,Constants.DIRECT_EXCHANGE_NAME,"a");
        channel.queueBind(Constants.DIRECT_QUEUE_NAME2,Constants.DIRECT_EXCHANGE_NAME,"a");
        channel.queueBind(Constants.DIRECT_QUEUE_NAME2,Constants.DIRECT_EXCHANGE_NAME,"b");
        channel.queueBind(Constants.DIRECT_QUEUE_NAME2,Constants.DIRECT_EXCHANGE_NAME,"c");

        String msg="消息的发送 + direct +  a   ";
        String msg1="消息的发送 + direct +  b   ";
        String msg2="消息的发送 + direct +  c   ";

        channel.basicPublish(Constants.DIRECT_EXCHANGE_NAME,"a",null,msg.getBytes());
        channel.basicPublish(Constants.DIRECT_EXCHANGE_NAME,"b",null,msg1.getBytes());
        channel.basicPublish(Constants.DIRECT_EXCHANGE_NAME,"c",null,msg2.getBytes());
//        System.out.println("消息发送成功");
        channel.close();
        connection.close();

    }
}
